package paginas;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ResourceBundle;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import modelUtil.Configuracion;
import modelUtil.Util;
import plantilla.BotonTO;
import util.BotonPersonalizado;
import util.TransicionBotones;
import AccionBoton.AccionCerrar;
import AccionBoton.AccionFocoCreditos;
import AccionBoton.AccionGuardarAspectoBoton;

public class PaginaAspectoBoton extends JDialog{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private PaginaEntornoCreacionTeclados _frame;
	private BotonTO _botonTO;
	private BotonPersonalizado _boton;
	private JPanel panel = new JPanel();
	private JPanel panel1 = new JPanel();
	private JPanel panel2 = new JPanel();
	private JPanel panel3 = new JPanel();
	private JPanel panel4 = new JPanel();
	private JPanel tab1,tab2;
	private EditorAspectoBotonTab3 tab3;
	private BotonPersonalizado b1,b2;
	private TransicionBotones policy;

	
	
	public PaginaAspectoBoton(PaginaEntornoCreacionTeclados frame){
		
		_frame = frame;
		_botonTO = (BotonTO)_frame.getBotonseleccionado().getBotonInfo().Clone();
		_boton = Util.BotonImagen(_botonTO);

		_boton.setFocusable(false);
		initComponents();
		
        Vector<Component> order = new Vector<Component>(4);
        order.add(tab1);
        order.add(tab2);
        order.add(tab3);
        order.add(b1);
        order.add(b2);

        _frame.addWindowFocusListener(new WindowAdapter() {
    	    public void windowGainedFocus(WindowEvent e) {
    		    tab1.requestFocusInWindow(); 
    	    }
    	});
       _frame.setFocusable(true);
       policy = new TransicionBotones(order);
       _frame.setFocusTraversalPolicy(policy);
		setModal(true);
		setAlwaysOnTop(true);
		setBounds(0, 0, 700, 600);
		setLocationRelativeTo(null);
		this.pack();
		this.repaint();
		setVisible(true);
		
	}
	
	
	public void initComponents(){
		
		GridBagLayout grid = new GridBagLayout();
		GridBagConstraints cons = new GridBagConstraints();
		add(panel);
		panel.setLayout(grid);
		cons.gridx = 0;
		cons.gridy = 0;
		cons.gridheight = 1;
		cons.gridwidth = 1;
		cons.fill = GridBagConstraints.BOTH;
		crearPanel1();
		crearPanel2();
		grid.addLayoutComponent(panel1, cons);
		panel.add(panel1);
		panel2.setPreferredSize(new Dimension(700,150));

		cons.gridy = 1;
		grid.addLayoutComponent(panel2, cons);
		panel.add(panel2);


		
	}
	
	
	public void crearPanel1(){
		JTabbedPane panel = new JTabbedPane();
		tab1 = new EditorAspectoBotonTab1(this);
		tab2 = new EditorAspectoBotonTab2(this);
		tab3 = new EditorAspectoBotonTab3(this);
		tab1.setFocusable(true);
		tab2.setFocusable(true);
		tab3.setFocusable(true);
		panel.addTab(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_pagina_aspecto_boton_texto_tab"),tab1);
		panel.addTab(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_pagina_aspecto_boton_imagen_tab"), tab2);
		panel.addTab(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_pagina_aspecto_boton_accesibilidad_tab"), tab3);
		panel1.add(panel);
		
	}
	
	public void crearPanel2(){
		

		b1 = Util.BotonImagen(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_popup_admin_aceptar"), "lib//Aceptar.png");
		b1.setPreferredSize(new Dimension(150,50));
		b1.getLabelTexto().setFont(b1.getLabelTexto().getFont().deriveFont(Font.BOLD, 14));
		b1.getLabelImagen().setHorizontalAlignment(SwingConstants.LEFT);
		b1.getLabelTexto().setHorizontalAlignment(SwingConstants.RIGHT);
		b1.setBackground(new Color(238,238,238));
		b1.setFocusable(true);
		b1.setMnemonic(KeyEvent.VK_A);
		b1.getLabelTexto().setDisplayedMnemonic(KeyEvent.VK_A);
		b1.getLabelTexto().getAccessibleContext().setAccessibleName(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_accesible_aceptar_aspecto_boton"));
		b1.setToolTipText(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_accesible_aceptar_aspecto_boton"));
		b1.addFocusListener(new AccionFocoCreditos(b1));
		b1.addActionListener(new AccionGuardarAspectoBoton(this));
		
		b2 = Util.BotonImagen(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_popup_admin_cancelar"), "lib//cancelar.png");
		b2.addActionListener(new AccionCerrar(this,_frame));
		b2.setPreferredSize(new Dimension(150,50));
		b2.getLabelImagen().setHorizontalAlignment(SwingConstants.LEFT);
		b2.getLabelTexto().setHorizontalAlignment(SwingConstants.RIGHT);
		b2.getLabelTexto().setFont(b2.getLabelTexto().getFont().deriveFont(Font.BOLD, 14));
		b2.setBackground(new Color(238,238,238));
		b2.setFocusable(true);
		b2.setMnemonic(KeyEvent.VK_C);
		b2.getLabelTexto().setDisplayedMnemonic(KeyEvent.VK_C);
		b2.getLabelTexto().getAccessibleContext().setAccessibleName(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_accesible_cancelar_aspecto_boton"));
		b2.setToolTipText(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_accesible_cancelar_aspecto_boton"));
		b2.addFocusListener(new AccionFocoCreditos(b2));
		b2.addActionListener(new AccionCerrar(this,_frame));
		panel4.add(b1);
		panel4.add(b2);
		

		
		TitledBorder borde = new TitledBorder(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_pagina_aspecto_boton_titulo"));
		borde.setTitleFont(borde.getTitleFont().deriveFont(Font.BOLD, 14));
		panel3.setBorder(borde);
		panel3.setPreferredSize(new Dimension(200,80));
		
		
    	int alto = _boton.getLabelImagen().getIcon().getIconHeight();
    	int ancho = _boton.getLabelImagen().getIcon().getIconWidth();
    	
    	double factorEscala = (alto/(double)((80*80)/100));

		int anchoImagen = ((int) (ancho / factorEscala));
		int altoImagen = ((int) (80*80)/100);
		
		ImageIcon imagenicono;
		if(_boton.getBotonInfo().getNombreIconoFichero().contains("/")){
    		imagenicono = new ImageIcon(_boton.getBotonInfo().getNombreIconoFichero());

		}else{
			
		imagenicono = new ImageIcon(Configuracion.instancia().getDirUsuarioActual() + "//" +Configuracion.instancia().getTecladoActual() + "//" + _boton.getBotonInfo().getNombreIconoFichero());
		
		}

		//ancho = ((int) (ancho / factorEscala));
		//alto = ((int) (alto / factorEscala));
		Image redimensionada = imagenicono.getImage().getScaledInstance(anchoImagen,
				altoImagen, Image.SCALE_DEFAULT);
		imagenicono = new ImageIcon(redimensionada);
		_boton.getLabelImagen().setIcon(imagenicono);
		GridBagLayout grid2 = new GridBagLayout();
		GridBagConstraints cons2 = new GridBagConstraints();
		panel3.setLayout(grid2);
		cons2.gridx = 0;
		cons2.gridy = 0;
		cons2.gridheight = 1;
		cons2.gridwidth = 1;
        cons2.weightx = 1.0;
        cons2.weighty = 1.0;
		cons2.fill = GridBagConstraints.BOTH;
		grid2.addLayoutComponent(_boton, cons2);
		panel3.add(_boton);
		
		
		GridBagLayout grid = new GridBagLayout();
		GridBagConstraints cons = new GridBagConstraints();
		panel2.setLayout(grid);
		cons.gridx = 0;
		cons.gridy = 0;
		cons.gridheight = 1;
		cons.gridwidth = 1;
		cons.insets = new Insets(0, 60, 0, 0);
		cons.fill = GridBagConstraints.BOTH;
		grid.addLayoutComponent(panel3, cons);
		panel2.add(panel3);
		cons.insets = new Insets(0, 0, 0, 0);
		cons.gridy = 1;
		cons.gridwidth = 2;

		grid.addLayoutComponent(panel4, cons);
		panel2.add(panel4);
		
	}


	public BotonTO get_botonTO() {
		return _botonTO;
	}


	public void set_botonTO(BotonTO botonTO) {
		_botonTO = botonTO;
	}


	public BotonPersonalizado get_boton() {
		return _boton;
	}


	public void set_boton(BotonPersonalizado boton) {
		_boton = boton;
	}


	public PaginaEntornoCreacionTeclados get_frame() {
		return _frame;
	}


	public void set_frame(PaginaEntornoCreacionTeclados frame) {
		_frame = frame;
	}

	
	public EditorAspectoBotonTab3 getTab3() {
		return tab3;
	}


	public void setTab3(EditorAspectoBotonTab3 tab3) {
		this.tab3 = tab3;
	}

	
}
